import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

import de.tbmx.api.*;




/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TradingUI extends JFrame implements StatusListener,FocusListener {
	private JPanel panel;
	private ClientAPI client;
	private TextPanel marketLogs = new TextPanel("Market Logs", false);
; 
	private OrderManager orderManager = new OrderManager();

	private OrderInfo orderInfo = new OrderInfo();
	private JTextField symbolTxt;
	private String symbol;
	private JTextField hedgeSymbolTxt;
	private String hedgeSymbol;
	private JTextField exchangeTxt;
	private String exchange;
	private JTextField hedgeExchangeTxt;
	private String hedgeExchange;
	private JTextField orderSizeTxt;
	private JTextField hedgeSizeTxt;
	private JTextField baseSpreadTxt;
	private JTextField triggerSpreadTxt;
	private JTextField minStockSizeTxt;
	private JTextField baseMove;
	private JTextField hedgeMove;

	
	TradingUI()
	{
		//setLocation(0, -15);
		setResizable(false);
		getContentPane().setLayout(null);
		this.setPreferredSize(new java.awt.Dimension(1165, 452));
		this.setSize(1165, 452);
//		setSize(600, 700);
		panel = createButtonPanel();
		getContentPane().add(panel);
	//	panel.setPreferredSize(new Dimension(100,400));
		//panel.setLocation(610, 0);
	
		{
//			marketLogs.setLayout(null);
			marketLogs.setBounds(0, 266, 1149, 142);
			getContentPane().add(marketLogs);
//			marketLogs.setPreferredSize(new Dimension(600, 100));
		}
		
	
		
		client = new ClientAPI();
		client.addStatusListener(this);
	//	orderManager.setApi(client);
		orderManager.setLog(marketLogs);
		orderInfo.setOrderManager(orderManager);
		client.initiateConnection( true );
		client.addPositionListener( orderManager);

		JTable t = new JTable(orderManager);
		t.getColumn("Eye ON/OFF").setCellRenderer(new ButtonRenderer());
		t.getColumn("Eye ON/OFF").setCellEditor(new ButtonEditor(new JCheckBox()));
		t.getColumn("Delete").setCellRenderer(new DeleteSpreadRenderer());
		t.getColumn("Delete").setCellEditor(new DeleteSpreadButton(new JCheckBox()));
		SpreadSpinner s1 = new SpreadSpinner();
		SpreadSpinner s2 = new SpreadSpinner();
		t.getColumn("Low Spread").setCellEditor(s1);
		t.getColumn("Low Spread").setCellRenderer(s1);
		t.getColumn("High Spread").setCellEditor(s2);
		t.getColumn("High Spread").setCellRenderer(s2);
		TickONOFF tick = new TickONOFF(new JCheckBox());
		t.getColumn("Tick ON/OFF").setCellEditor(tick);
		t.getColumn("Tick ON/OFF").setCellRenderer(tick);
		tick = new TickONOFF(new JCheckBox());
		t.getColumn("b=a ON/OFF").setCellEditor(tick);
		t.getColumn("b=a ON/OFF").setCellRenderer(tick);
		tick = new TickONOFF(new JCheckBox());
		t.getColumn("N/A ON/OFF").setCellEditor(tick);
		t.getColumn("N/A ON/OFF").setCellRenderer(tick);
		tick = new TickONOFF(new JCheckBox());
		t.getColumn("Trade Phase").setCellEditor(tick);
		t.getColumn("Trade Phase").setCellRenderer(tick);
		tick = new TickONOFF(new JCheckBox());
		t.getColumn("Auction ON/OFF").setCellEditor(tick);
		t.getColumn("Auction ON/OFF").setCellRenderer(tick);
		
		DateSpinner date = new DateSpinner();
		t.getColumn("Auction Time").setCellEditor(date);
		t.getColumn("Auction Time").setCellRenderer(date);
		
		SpreadSpinner baseDiff = new SpreadSpinner();
		SpreadSpinner hedgeDiff = new SpreadSpinner();
		SpreadSpinner auctionOffset = new SpreadSpinner();

		t.getColumn("Base Diff").setCellEditor(baseDiff);
		t.getColumn("Base Diff").setCellRenderer(baseDiff);
		t.getColumn("Hedge Diff").setCellEditor(hedgeDiff);
		t.getColumn("Hedge Diff").setCellRenderer(hedgeDiff);
		t.getColumn("Auction Offset").setCellEditor(auctionOffset);
		t.getColumn("Auction Offset").setCellRenderer(auctionOffset);
		
		t.setAutoscrolls(true);
		t.setAutoResizeMode( JTable.AUTO_RESIZE_OFF);
		TableColumnModel tcm = t.getColumnModel();
		int width[] = {60, 60, 60, 60, 100, 100, 100, 70,70,70,70,70,70,70,70,100,80,70,70,80,80,80,100,100};
		for( int i=0; i < tcm.getColumnCount(); i++){
			tcm.getColumn(i).setPreferredWidth( width[i] );
		}
		t.doLayout();
		
		JScrollPane portPane = new JScrollPane(t); 
		portPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		portPane.setBounds(10, 77, 1159, 200);
		portPane.setBorder(BorderFactory.createTitledBorder( "Spreader List") );
	//	portPane.setPreferredSize(new  Dimension(400,200));
		getContentPane().add(portPane);

		//	portPane.setBounds(10, 300, 780, 200);
		
	}
	private JPanel createButtonPanel()
	{
		JPanel buttonPanel = new JPanel( );
		buttonPanel.setBounds(0, 0, 1159, 67);
		buttonPanel.setLayout(null);
		symbolTxt = new JTextField();
		symbolTxt.setLocation(88, 2);
	    symbolTxt.setFocusable(true);
	 //   symbolTxt.addFocusListener(this);  
		buttonPanel.add(symbolTxt);
	    symbolTxt.setSize(49,21);
		JLabel l2 = new JLabel("Exchange", SwingConstants.LEFT);
		l2.setBounds(146, 0, 60, 25);
	//	l2.setSize(30, 30);
		buttonPanel.add(l2);
		exchangeTxt = new JTextField();
		exchangeTxt.setBounds(253, 2, 49, 21);
	//	exchangeTxt.addFocusListener(this);
		buttonPanel.add(exchangeTxt);
		JLabel l3 = new JLabel("Hedge SYM", SwingConstants.LEFT);
		l3.setBounds(10, 35, 69, 25);
		buttonPanel.add(l3);
		hedgeSymbolTxt = new JTextField("");
		hedgeSymbolTxt.setBounds(88, 37, 49, 21);
	//	hedgeSymbolTxt.addFocusListener(this);
		buttonPanel.add(hedgeSymbolTxt);
		JLabel l4 = new JLabel("Hedge Exchange", SwingConstants.LEFT);
		l4.setBounds(146, 35, 102, 25);
		buttonPanel.add(l4);
		hedgeExchangeTxt = new JTextField();
		hedgeExchangeTxt.setBounds(253, 37, 49, 21);
	//	hedgeExchangeTxt.addFocusListener(this);
		buttonPanel.add(hedgeExchangeTxt);
		JLabel l5 = new JLabel("Size", SwingConstants.LEFT);
		l5.setBounds(308, 0, 49, 25);
		buttonPanel.add(l5);
		orderSizeTxt = new JTextField();
		orderSizeTxt.setBounds(380, 2, 67, 21);
	//	orderSizeTxt.addFocusListener(this);
		buttonPanel.add(orderSizeTxt);
		JLabel l6 = new JLabel("Hedge Size", SwingConstants.LEFT);
		l6.setBounds(308, 35, 69, 25);
		buttonPanel.add(l6);
		hedgeSizeTxt = new JTextField();				
		hedgeSizeTxt.setBounds(380, 37, 67, 21);
	//	hedgeSizeTxt.addFocusListener(this);
		buttonPanel.add(hedgeSizeTxt);
		JLabel l7 = new JLabel("Low Spread", SwingConstants.LEFT);
		l7.setBounds(630, 2, 74, 25);
		buttonPanel.add(l7);
		baseSpreadTxt = new JTextField();
		baseSpreadTxt.setBounds(714, 4, 49, 21);
	//	baseSpreadTxt.addFocusListener(this);
		
		buttonPanel.add(baseSpreadTxt);
		JLabel l8 = new JLabel("High Spread", SwingConstants.LEFT);
		l8.setBounds(626, 37, 78, 25);
		buttonPanel.add(l8);
		triggerSpreadTxt = new JTextField();
		triggerSpreadTxt.setBounds(714, 39, 49, 21);
	//	baseSpreadTxt.addFocusListener(this);
		buttonPanel.add(triggerSpreadTxt);
		JLabel l9 = new JLabel("Min Size");
		l9.setBounds(773, 37, 50, 25);
		buttonPanel.add(l9);
		minStockSizeTxt = new JTextField();
		minStockSizeTxt.setBounds(833, 39, 50, 21);
		buttonPanel.add(minStockSizeTxt);
		
		baseMove = new JTextField();
		baseMove.setBounds(536, 2, 66, 21);
		buttonPanel.add(baseMove);
		baseMove.setColumns(10);
		
		hedgeMove = new JTextField();
		hedgeMove.setBounds(536, 37, 66, 21);
		buttonPanel.add(hedgeMove);
		hedgeMove.setColumns(10);
				
		symbolTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( symbolTxt.getText().length() > 0){
					orderInfo.m_symbol = symbolTxt.getText();
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( symbolTxt.getText().length() > 0){
					orderInfo.m_symbol = symbolTxt.getText(); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
	    
		exchangeTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( exchangeTxt.getText().length() > 0){
					orderInfo.m_exchange = exchangeTxt.getText();
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( exchangeTxt.getText().length() > 0){
					orderInfo.m_exchange = exchangeTxt.getText(); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		hedgeSymbolTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeSymbolTxt.getText().length() > 0){
					orderInfo.m_hedgeSymbol= hedgeSymbolTxt.getText();
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeSymbolTxt.getText().length() > 0){
					orderInfo.m_hedgeSymbol = hedgeSymbolTxt.getText(); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		hedgeExchangeTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeExchangeTxt.getText().length() > 0){
					orderInfo.m_hedgeExchange= hedgeExchangeTxt.getText();
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeExchangeTxt.getText().length() > 0){
					orderInfo.m_hedgeExchange = hedgeExchangeTxt.getText(); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		orderSizeTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( orderSizeTxt.getText().length() > 0){
					orderInfo.m_size= Integer.parseInt(orderSizeTxt.getText());
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( orderSizeTxt.getText().length() > 0){
					orderInfo.m_size = Integer.parseInt(orderSizeTxt.getText()); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		hedgeSizeTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeSizeTxt.getText().length() > 0){
					orderInfo.m_hedgeSize= Integer.parseInt(hedgeSizeTxt.getText());
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeSizeTxt.getText().length() > 0){
					orderInfo.m_hedgeSize = Integer.parseInt(hedgeSizeTxt.getText()); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		baseSpreadTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( baseSpreadTxt.getText().length() > 0){
					orderInfo.m_lowSpread= Double.parseDouble(baseSpreadTxt.getText());
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( baseSpreadTxt.getText().length() > 0){
					orderInfo.m_lowSpread = Double.parseDouble(baseSpreadTxt.getText()); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		triggerSpreadTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( triggerSpreadTxt.getText().length() > 0){
					orderInfo.m_highLpread= Double.parseDouble(triggerSpreadTxt.getText());
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( triggerSpreadTxt.getText().length() > 0){
					orderInfo.m_highLpread = Double.parseDouble(triggerSpreadTxt.getText()); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		minStockSizeTxt.getDocument().addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( minStockSizeTxt.getText().length() > 0){
					orderInfo.m_minStockSize= Integer.parseInt(minStockSizeTxt.getText());
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( minStockSizeTxt.getText().length() > 0){
					orderInfo.m_minStockSize = Integer.parseInt(minStockSizeTxt.getText()); 
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		baseMove.getDocument().addDocumentListener( new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( baseMove.getText().length() > 0){
					orderInfo.m_baseDiff = Double.parseDouble( baseMove.getText());
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( baseMove.getText().length() > 0){
					orderInfo.m_baseDiff = Double.parseDouble( baseMove.getText());
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		hedgeMove.getDocument().addDocumentListener( new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeMove.getText().length() > 0){
					orderInfo.m_hedgeDiff = Double.parseDouble( hedgeMove.getText());
				}
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				if ( hedgeMove.getText().length() > 0){
					orderInfo.m_hedgeDiff = Double.parseDouble( hedgeMove.getText());
					
				}
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
			}
		 });
		
		JLabel l1 = new JLabel("Base SYM", SwingConstants.LEFT);
		l1.setBounds(10, 0, 69, 25);
		buttonPanel.add(l1);
		{
			JButton btnEnable = new JButton("Add");
			buttonPanel.add(btnEnable);
			btnEnable.setBounds(913, 10, 83, 23);
			
			JLabel lblBaseMove = new JLabel("Base Move");
			lblBaseMove.setBounds(457, 5, 69, 15);
			buttonPanel.add(lblBaseMove);
			
			JLabel lblHedgeMove = new JLabel("Hedge Move");
			lblHedgeMove.setBounds(457, 40, 69, 15);
			buttonPanel.add(lblHedgeMove);
			
			JButton panicButton = new JButton("<html>Panic <br> Button</html>");
			
			panicButton.setBounds(1029, 3, 93, 57);
			buttonPanel.add(panicButton);
			
			panicButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {	
					orderManager.panicTriggered();
				}
			});
			
			btnEnable.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {			
					if (checkOrderInfo()) {
						OrderInfo o = new OrderInfo(orderInfo, marketLogs);
						orderManager.addOrder(o); 
						o.setOrderManager( orderManager );
						o.start(client );				
						
//						symbolTxt.setText("");
//						exchangeTxt.setText("");
//						hedgeSymbolTxt.setText("");
//						hedgeExchangeTxt.setText("");
//						orderSizeTxt.setText("");
//						hedgeSizeTxt.setText("");
//						baseSpreadTxt.setText("");
//						triggerSpreadTxt.setText("");
//						baseMove.setText("");
//						hedgeMove.setText("");
//						minStockSizeTxt.setText("");
						
					} else { //?
							marketLogs.add("!!!Failed to pass order checking, Please check the order infomation!!!");
					}
				}			
			});
		}
		

		//addPair.setBounds(30, 60, 130, 72);

		return buttonPanel;
	}
	
	private boolean checkOrderInfo()
	{
		if (orderInfo.m_symbol.equals("")) {
			return false;
		}
		if (orderInfo.m_hedgeSymbol.equals("")) {
			return false;
		} 
		if (orderInfo.m_exchange.equals("")) {
			return false;
		}
		if (orderInfo.m_hedgeExchange.equals("")) {
			return false;
		}
		if (orderInfo.m_size == 0) { 
			return false;
		}
		if (orderInfo.m_hedgeSize == 0) {
			return false;
		}
		if (orderInfo.m_lowSpread == 0) {
			return false;
		}
		if (orderInfo.m_highLpread == 0) {
			return false;
		}
		if ( orderInfo.m_baseDiff == 0 || orderInfo.m_hedgeDiff == 0){
			return false;
		}
		return true;
	}

	public void apiStatusChanged(boolean isConnected)
	{
		if (isConnected) {
			marketLogs.add("=== Client is connnected ===");
		} else {
			marketLogs.add("=== Client is disconnnected ===");
		}
	 
	}
	@Override
	public void focusGained(FocusEvent e) {
		
		// TODO Auto-generated method stub
		//((JTextField)e.getSource()).setText("");		
	}
	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub
		
	}
}


